package org.southyin.service.impl;

import org.southyin.commons.CommonsResp;
import org.southyin.dao.CmsCustormerDao;
import org.southyin.dao.impl.CmsCustormerDaoImpl;
import org.southyin.model.CmsCustomer;
import org.southyin.service.CmsCustomerService;
import org.southyin.utils.MD5;

/**
 * @ClassName CustomerServiceImpl
 * @Description TODO
 * @Author hasee
 * @Date 2021/10/27
 * @Version 1.0
 **/
public class CmsCustomerServiceImpl implements CmsCustomerService {
    CmsCustormerDao dao = new CmsCustormerDaoImpl();

    //注册
    public CommonsResp customerReg(CmsCustomer cmsCustomer) {
        //查询注册用户的邮箱是否被占用
        int count = dao.findCustomerByEmail(cmsCustomer.getEmail());
        if (count == 0) {
            //做注册
            cmsCustomer.setPassword(MD5.getMd5(cmsCustomer.getPassword()+cmsCustomer.getEmail()));//加密
            int i = dao.saveCustomer(cmsCustomer);
            if (i != 0) {
                return CommonsResp.success(200, "新增用户成功");
            }
        }
        return CommonsResp.success(500, "用户邮箱被占用");
    }

    public CommonsResp customerLogin(CmsCustomer cmsCustomer) {
        cmsCustomer.setPassword(MD5.getMd5(cmsCustomer.getPassword()+cmsCustomer.getEmail()));//加密
        CmsCustomer customer = dao.findCustomerByEmailAndPass(cmsCustomer);
        if (customer == null) {
            return CommonsResp.failed(500,"用户邮箱或密码错误");
        }
        if (!customer.getEnabled()){
            return CommonsResp.failed(500,"用户被禁用");
        }
        if (!customer.getDelMark()){
            return CommonsResp.failed(500,"用户被锁定");
        }
        return CommonsResp.failed(200,"登陆成功",customer);
    }

//    public CmsCustomer getPersonInfos(Long id) {
//        return dao.getPersonInfos(id);
//    }

}
